package com.dona.imsidetector.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IMSIUtil {

    public static String getMobileAll(String imsi) {
        String s130 = "^46001(\\d{3})(\\d)[0,1]\\d+";
        String s131 = "^46001(\\d{3})(\\d)9\\d+";
        String s132 = "^46001(\\d{3})(\\d)2\\d+";
        String s134 = "^460020(\\d)(\\d{3})\\d+";
        String s13x0 = "^46000(\\d{3})([5,6,7,8,9])\\d+";
        String s13x = "^46000(\\d{3})([0,1,2,3,4])(\\d)\\d+";
        String s150 = "^460023(\\d)(\\d{3})\\d+";
        String s151 = "^460021(\\d)(\\d{3})\\d+";
        String s152 = "^460022(\\d)(\\d{3})\\d+";
        String s155 = "^46001(\\d{3})(\\d)4\\d+";
        String s156 = "^46001(\\d{3})(\\d)3\\d+";
        String s157 = "^460077(\\d)(\\d{3})\\d+";
        String s158 = "^460028(\\d)(\\d{3})\\d+";
        String s159 = "^460029(\\d)(\\d{3})\\d+";
        String s147 = "^460079(\\d)(\\d{3})\\d+";
        String s185 = "^46001(\\d{3})(\\d)5\\d+";
        String s186 = "^46001(\\d{3})(\\d)6\\d+";
        String s187 = "^460027(\\d)(\\d{3})\\d+";
        String s188 = "^460078(\\d)(\\d{3})\\d+";
        String s1705 = "^460070(\\d)(\\d{3})\\d+";
        String s170x = "^46001(\\d{3})(\\d)8\\d+";
        String s178 = "^460075(\\d)(\\d{3})\\d+";
        String s145 = "^46001(\\d{3})(\\d)7\\d+";
        String s182 = "^460026(\\d)(\\d{3})\\d+";
        String s183 = "^460025(\\d)(\\d{3})\\d+";
        String s184 = "^460024(\\d)(\\d{3})\\d+";


        String s180 = "^46003(\\d)(\\d{3})7\\d+";
        String s153 = "^46003(\\d)(\\d{3})8\\d+";
        String s189 = "^46003(\\d)(\\d{3})9\\d+";


        String[] result = compile(s130, imsi);
        if ((result != null) && (result.length == 2)) {
            return "130" + result[1] + result[0];
        }
        result = compile(s131, imsi);
        if ((result != null) && (result.length == 2)) {
            return "131" + result[1] + result[0];
        }
        result = compile(s132, imsi);
        if ((result != null) && (result.length == 2)) {
            return "132" + result[1] + result[0];
        }
        result = compile(s134, imsi);
        if ((result != null) && (result.length == 2)) {
            return "134" + result[0] + result[1];
        }
        result = compile(s13x0, imsi);
        if ((result != null) && (result.length == 2)) {
            return "13" + result[1] + "0" + result[0];
        }
        result = compile(s13x, imsi);
        if ((result != null) && (result.length == 3)) {
            return "13" + (Integer.parseInt(result[1]) + 5) + result[2] + result[0];
        }
        result = compile(s150, imsi);
        if ((result != null) && (result.length == 2)) {
            return "150" + result[0] + result[1];
        }
        result = compile(s151, imsi);
        if ((result != null) && (result.length == 2)) {
            return "151" + result[0] + result[1];
        }
        result = compile(s152, imsi);
        if ((result != null) && (result.length == 2)) {
            return "152" + result[0] + result[1];
        }
        result = compile(s155, imsi);
        if ((result != null) && (result.length == 2)) {
            return "155" + result[1] + result[0];
        }
        result = compile(s156, imsi);
        if ((result != null) && (result.length == 2)) {
            return "156" + result[1] + result[0];
        }
        result = compile(s157, imsi);
        if ((result != null) && (result.length == 2)) {
            return "157" + result[0] + result[1];
        }
        result = compile(s158, imsi);
        if ((result != null) && (result.length == 2)) {
            return "158" + result[0] + result[1];
        }
        result = compile(s159, imsi);
        if ((result != null) && (result.length == 2)) {
            return "159" + result[0] + result[1];
        }
        result = compile(s147, imsi);
        if ((result != null) && (result.length == 2)) {
            return "147" + result[0] + result[1];
        }
        result = compile(s185, imsi);
        if ((result != null) && (result.length == 2)) {
            return "185" + result[1] + result[0];
        }
        result = compile(s186, imsi);
        if ((result != null) && (result.length == 2)) {
            return "186" + result[1] + result[0];
        }
        result = compile(s187, imsi);
        if ((result != null) && (result.length == 2)) {
            return "187" + result[0] + result[1];
        }
        result = compile(s188, imsi);
        if ((result != null) && (result.length == 2)) {
            return "188" + result[0] + result[1];
        }
        result = compile(s1705, imsi);
        if ((result != null) && (result.length == 2)) {
            return "170" + result[0] + result[1];
        }
        result = compile(s170x, imsi);
        if ((result != null) && (result.length == 2)) {
            return "170" + result[1] + result[0];
        }
        result = compile(s178, imsi);
        if ((result != null) && (result.length == 2)) {
            return "178" + result[0] + result[1];
        }
        result = compile(s145, imsi);
        if ((result != null) && (result.length == 2)) {
            return "145" + result[1] + result[0];
        }
        result = compile(s182, imsi);
        if ((result != null) && (result.length == 2)) {
            return "182" + result[0] + result[1];
        }
        result = compile(s183, imsi);
        if ((result != null) && (result.length == 2)) {
            return "183" + result[0] + result[1];
        }
        result = compile(s184, imsi);
        if ((result != null) && (result.length == 2)) {
            return "184" + result[0] + result[1];
        }
        result = compile(s180, imsi);
        if ((result != null) && (result.length == 2)) {
            return "180" + result[0] + result[1];
        }
        result = compile(s153, imsi);
        if ((result != null) && (result.length == 2)) {
            return "153" + result[0] + result[1];
        }
        return "";
    }


    private static String[] compile(String reg, String imsi) {
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(imsi);


        if (matcher.find()) {
            String[] sArr = new String[matcher.groupCount()];
            for (int i = 0; i < matcher.groupCount(); i++) {
                sArr[i] = matcher.group(i + 1);
            }
            return sArr;
        }
        return null;
    }

    public static String genFakerImsi(){
        // 460001357924680
        String[] head = {"46000", "46001", "46002", "46004", "46003"};
        String str="0123456789";
        Random random=new Random();
        int index = random.nextInt(5);
        StringBuffer sb=new StringBuffer();
        sb.append(head[index]);
        for(int i=0;i<10;i++){
            int number=random.nextInt(10);
            sb.append(str.charAt(number));
        }
        return sb.toString();

    }

    public static int checkImsiType(String IMSI) {

        // IMSI号前面3位460是国家，紧接着后面2位00 02 07是中国移动，01是中国联通，03是中国电信。
        if (IMSI.startsWith("46000") || IMSI.startsWith("46002") || IMSI.startsWith("46004") || IMSI.startsWith("46007") || IMSI.startsWith("46008")) {
            return 1;
        } else if (IMSI.startsWith("46001") || IMSI.startsWith("46006" )|| IMSI.startsWith("46009") || IMSI.startsWith("46010")) {
            return 2;
        } else if (IMSI.startsWith("46003")|| IMSI.startsWith("46005" )|| IMSI.startsWith("46011" ) || IMSI.startsWith("46012" )) {
            return 3;
        }
        return 4;
    }

    public static String tranferImsiType(int type) {
        if (type == 1) {
            return "移动";
        } else if (type == 2) {
           return "联通";
        } else if (type == 3) {
            return "电信";
        }else {
            return "其他";
        }
    }

    public static String getISPbyImsi(String IMSI){
        if (IMSI.startsWith("46000") || IMSI.startsWith("46002") || IMSI.startsWith("46004") || IMSI.startsWith("46007") || IMSI.startsWith("46008")) {
            return "移动";
        } else if (IMSI.startsWith("46001") || IMSI.startsWith("46006" )|| IMSI.startsWith("46009") || IMSI.startsWith("46010")) {
            return "联通";
        } else if (IMSI.startsWith("46003")|| IMSI.startsWith("46005" )|| IMSI.startsWith("46011" ) || IMSI.startsWith("46012" )) {
            return "电信";
        }
        return "其它";
    }


    public static String getData(String data,String from){
        //103 OneUeInfoIndi PS_WL S2A4A2109059 time[296] taType[MEAS_RPT] imsi[460023148087839] imei[000000000000000] ulSig[93] ulTa[13] bl_indi[1]
        String newData = data.substring(data.indexOf(from) + from.length() + 1);
        String result = newData.substring(0,newData.indexOf("]"));
        return result;
    }


    public static  Map<String,List<String>> plmns = new HashMap<>();

    static {

        List<String> strings = new ArrayList<>();
        strings.add("46000");
//        strings.add("46002");
//        strings.add("46004");
//        strings.add("46007");
//        strings.add("46008");

        plmns.put("移动",strings);

        strings = new ArrayList<>();
        strings.add("46001");
//        strings.add("46006");
//        strings.add("46009");
//        strings.add("46010");

        plmns.put("联通",strings);

        strings = new ArrayList<>();
//        strings.add("46003");
//        strings.add("46005");
        strings.add("46011");
//        strings.add("46012");

        plmns.put("电信",strings);
    }


}
